package com.study.zzedu.ui.tuitioninfo;

import android.graphics.Rect;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;

import com.study.zzedu.R;
import com.study.zzedu.adapter.TuitionAdapter;
import com.study.zzedu.base.BaseMvpActivity;
import com.study.zzedu.base.BaseView;
import com.study.zzedu.entity.TuitionModel;
import com.study.zzedu.utils.LogUtil;
import com.study.zzedu.utils.ToastUtil;
import com.study.zzedu.widget.SpacesItemDecoration;

import org.json.JSONArray;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

public class TuitionInfoActivity extends BaseMvpActivity<BaseView, TuitionInfoPresenter> implements BaseView {

    private List<TuitionModel> dataArray = new ArrayList<>();
    private RecyclerView recycler;

    @Override
    protected int getLayoutID() {
        return R.layout.activity_tuition_info;
    }

    @Override
    protected TuitionInfoPresenter createPresenter() {
        return new TuitionInfoPresenter();
    }

    @Override
    protected BaseView createView() {
        return this;
    }


    @Override
    protected void initView() {
        //加载数据,自定义函数在函数中加载需要显示的数据
        recycler = (RecyclerView) findViewById(R.id.rv_tuition_list);
    }

    @Override
    protected void initData() {
        LinearLayoutManager layoutmanager = new LinearLayoutManager(this);
        recycler.setLayoutManager(layoutmanager);
        getPresenter().getTuitionInfo(TuitionInfoActivity.this);
    }

    @Override
    protected void initListener() {
        findViewById(R.id.rl_back).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
    }

    @Override
    public void getData(String data) {
        try {
            JSONObject jObject = new JSONObject(data);
            JSONArray jsonArray = jObject.getJSONArray("data");

            for (int i = 0; i < jsonArray.length(); i++) {
                JSONObject job = jsonArray.getJSONObject(i);

                dataArray.add(new TuitionModel(job.getString("xmmc"), job.getString("bz2"), job.getString("lrsj"), job.getString("sjje"),job.getString("tfje")));
            }

            if (!(jsonArray.length() > 0)) {
                ToastUtil.showShortToast("无数据");
                return;
            }

            TuitionAdapter dapter = new TuitionAdapter(dataArray);//m是集合，上面应该定义一个集合
            recycler.addItemDecoration(new SpacesItemDecoration(30));
            recycler.setAdapter(dapter);
        } catch (Exception e) {
            LogUtil.e("Exception", e.getMessage());
        }
    }

    @Override
    public void showError(String error) {
        if (null != error && !TextUtils.isEmpty(error)) {
            ToastUtil.showShortToast(error);
        }
    }

}